package com.seatademo.orders.controller;

import com.seatademo.orders.service.IOrderTblService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/orders")
public class OrderController {

    //注入Order业务类

    //controller实现订单保存

    @RequestMapping("/save")
    public boolean save(){
        System.out.println("保存订单成功");
        return true;
    }

    @Autowired
    private IOrderTblService iOrderTblService;

    /**
     * 下单 模拟库存减少
     * @return
     */
    @GlobalTransactional
    @RequestMapping("/placeOrder/commit")
    public Boolean placeOrderCommit(){
        iOrderTblService.placeOrder1("1", "product-1", 1);
        return true;
    }

    /**
     * 下单 模拟回滚
     * @return
     */
    @RequestMapping("/placeOrder/rollback")
    public Boolean placeOrderRollback() {
        // product-2 扣库存时模拟了一个业务异常,
        iOrderTblService.placeOrder2("1", "product-2", 1);
        return true;
    }


}
